/*
 * lib2Desa - Another 2D game library in C++
 * Copyright (C) 2009 Erivaldo Xavier de Lima Filho
 *
 * This program is free software and is distributed under GNU General Public License version 3. You can modify and/or redistribute
 * it under the terms of GPL version 3 or posterior version. This program comes with NO WARRANTY, for more details, see LICENSE,
 * or <http://www.gnu.org/licenses>.
 *
 * Contact info:
 *
 *	Erivaldo Xavier de Lima Filho, desadoc@gmail.com
 *
 */

#include "Core/GameObject.hpp"

using namespace std;

#ifndef _NO_NAMESPACE
namespace lib2Desa {
#endif

GameObject::~GameObject(void)
{
	animations.clear();
}

void GameObject::draw(const shared_ptr<IGraphicDevice>& gd) const
{
	list<u16>::const_iterator it = enabled.begin();

	for(; it != enabled.end(); it++)
	{
		animations[(*it)]->draw(gd);
	}
}

bool GameObject::run(u32 ml)
{
	list<u16>::iterator it = enabled.begin();

	for(; it != enabled.end(); it++)
	{
		animations[(*it)]->run(ml);
	}

	return true;
}

void GameObject::setAnimation(u16 index, const shared_ptr<Animation>& ani)
{
	if(index>=animations.size())
	{
		u16 old_size = animations.size();

		animations.resize(index+1);

		for(;old_size < index+1; old_size++)
		{
			animations[old_size] = null;
		}
	}

	animations[index] = new Animation(*ani);
	animations[index]->setParent(this);
}

shared_ptr<Animation> GameObject::getAnimation(u16 index) const
{
	if(index < animations.size())
	{
		return animations[index];
	} else {
		return shared_ptr<Animation>();
	}
}

void GameObject::enableAnimation(u16 index, bool enable)
{
	if(index >= animations.size())
	{
		return;
	}

	if(enable)
	{
		enabled.remove(index);
		enabled.push_back(index);
	}
	else
	{
		enabled.remove(index);
	}
}

void GameObject::notify(shared_ptr<IEvent> e)
{
	e->consume();
}

#ifndef _NO_NAMESPACE
}
#endif
